package cs6238.server;

import java.util.UUID;

import cs6238.common.FileInfo;
import cs6238.common.Guard;

/**
 * Used for storing info on stored files, can be read to/from disk
 */
public class FileEntry {

	private String _hostname;
	private UUID _fileId;
	private String _encryptedFilehash;
	private String _clientFilename;
	
	public FileEntry(String hostname, UUID fileId, String encryptedFilehash, String clientFilename){
		_hostname = hostname;
		_fileId = fileId;
		_encryptedFilehash = encryptedFilehash;
		_clientFilename = clientFilename;
	}
	
	public FileEntry(String csvLine){
		String[] values = csvLine.split(",");
		Guard.that(values.length == 4, "Invalid FileMetadata CSV");
		
		_hostname = values[0];
		_fileId = UUID.fromString(values[1]);
		_encryptedFilehash = values[2];
		_clientFilename = values[3];
	}
	
	public static String toCsvColumns() {
		return "hostname,FileId,encryptedFilehash,clientFilename";
	}
	
	public String toCSV(){
		StringBuilder builder = new StringBuilder();
		char comma = ',';
		builder.append(_hostname);						builder.append(comma);
		builder.append(_fileId.toString());		builder.append(comma);
		builder.append(_encryptedFilehash);						builder.append(comma);
		builder.append(_clientFilename);
		
		return builder.toString();
	}
	
	public String getHostname() {
		return _hostname;
	}

	public UUID getFileId() {
		return _fileId;
	}

	public String getEncryptedFileHash() {
		return _encryptedFilehash;
	}

	public String getClientFilename() {
		return _clientFilename;
	}
	
	public FileInfo createFileInfo(){
		FileInfo result = new FileInfo();
		result.setFileId(_fileId);
		result.setFilename(_clientFilename);
		return result;
	}
}
